package cn.ch.pet.service.impl;

import cn.ch.basic.service.impl.BaseServiceImpl;
import cn.ch.pet.domain.OrderPetAcquisition;
import cn.ch.pet.domain.Pet;
import cn.ch.pet.domain.SearchMasterMsg;
import cn.ch.pet.dto.OrderConfirmDto;
import cn.ch.pet.mapper.OrderPetAcquisitionMapper;
import cn.ch.pet.mapper.PetMapper;
import cn.ch.pet.mapper.SearchMasterMsgMapper;
import cn.ch.pet.service.IOrderPetAcquisitionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Why
 * @since 2022-02-27
 */
@Service
public class OrderPetAcquisitionServiceImpl extends BaseServiceImpl<OrderPetAcquisition> implements IOrderPetAcquisitionService {

    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private PetMapper petMapper;

    @Override
    public void removeOrder(Long id) {
        OrderPetAcquisition order = orderPetAcquisitionMapper.loadById(id);
        //1.订单取消
        order.setState(-1);
        orderPetAcquisitionMapper.update(order);
        //2.把订单对应的寻主消息 还到寻主池
        SearchMasterMsg msg = searchMasterMsgMapper.loadById(order.getSearchMasterMsgId());
        msg.setShopId(null);//置空消息
        searchMasterMsgMapper.update(msg);
    }

    @Override
    public void confirm(OrderConfirmDto dto) {
        //1.修改订单状态  + 填写实际收购金额  + 填写支付方式
        OrderPetAcquisition order = orderPetAcquisitionMapper.loadById(dto.getId());
        order.setState(1);
        order.setPaytype(dto.getPayType());
        order.setPrice(dto.getMoney());
        orderPetAcquisitionMapper.update(order);
        //2.对应寻主消息的状态  和钱钱改了
        SearchMasterMsg msg = searchMasterMsgMapper.loadById(order.getSearchMasterMsgId());
        msg.setPrice(dto.getMoney().doubleValue());
        msg.setState(2);
        searchMasterMsgMapper.update(msg);
        //3.对应宠物的成本价也要改了
        Pet pet = petMapper.loadById(order.getPetId());
        pet.setCostprice(dto.getMoney());
        petMapper.update(pet);
        //@TODO 4.不同的支付方式要走一些其他特定逻辑

    }

}
